package com.example.youyouhaodian.controller;


import com.example.youyouhaodian.pojo.Seclillgoods;
import com.example.youyouhaodian.service.SeckillgoodsService;
import com.example.youyouhaodian.util.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.Set;


import com.alibaba.fastjson.JSONObject;

@RestController
@RequestMapping(value = "/seckill")
public class RedisController {
    @Autowired
   RedisUtil redisUtil ;

    @Autowired
     SeckillgoodsService seckillgoodsService;


    //0测试连接redis
    @RequestMapping(value = "/test",method = RequestMethod.GET)
    public String demoTest(){

        redisUtil.set("1","123456");
        return "1";
    }

    //初始化系统静态资源写入redis
    @RequestMapping(value = "/add.html",method = RequestMethod.GET)
    public String addredis(){

        seckillgoodsService.set();
        return "写入成功";
    }



    //查询全部秒杀商品，判断时间
    @RequestMapping(value = "/seclectall.html",method = RequestMethod.GET)
    public String seclectgoods(){






        return "查询成功";
    }


    //查看单个秒杀商品详细信息
    @RequestMapping(value = "/seclect.html/{goodsid}",method = RequestMethod.GET)
    public JSONObject seclectgoods( @PathVariable("goodsid") String goodsid){
        JSONObject result = new JSONObject();
        Set<Seclillgoods> SET=seckillgoodsService.goods(goodsid);
        for(Seclillgoods SD: SET){
           result.put("scegoods",SD);
        }

        return result;
    }

    //开始秒杀,消息队列排队
    @RequestMapping(value = "/secgoods.html/{goodsid}",method = RequestMethod.GET)
    public JSONObject secgoods(@PathVariable("goodsid") String goodsid){
        //传入商品id和用户名id

        JSONObject result = new JSONObject();
        String  re=seckillgoodsService.secgoods(goodsid);

            result.put("result",re);

        return result;
    }



    //前台轮询秒杀情况,成功跳转购物车，失败返回结果
    @RequestMapping(value = "/secresult.html",method = RequestMethod.GET)
    public JSONObject secresult(@PathVariable("goodsid") String goodsid){

//传入商品id和用户名id

        JSONObject result = new JSONObject();
        Set<Seclillgoods> SET=seckillgoodsService.goods(goodsid);
        for(Seclillgoods SD: SET){

            result.put("scegoods",SD);
        }

        return result;
    }





    //查看购物车，超时2分钟未付款，自动取消订单/已付款写入数据库/数据库库存减一
    @RequestMapping(value = "/seclect.html",method = RequestMethod.GET)
    public String seclectgondsshop(){



        return "支付成功";
    }


    //查看订单
    @RequestMapping(value = "/seclectoeder.html",method = RequestMethod.GET)
    public String seclectoeder(){



        return "写入成功";
    }

}
